/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Rules;
import org.htmlparser.Tag;

import Database.*;

/**
 *
 * @author Sinem
 */
public class AttributeRule extends BaseRule{   
   
   public AttributeRule(Tag tag)
   {
       super();       
       m_Tag = tag;
   }

   @Override
   public void check()
   {
       boolean bLowerCase = true;
       String attribute;
       ResultTableItem result = new ResultTableItem();
       result.sWebPage = this.sWebPage;
       result.nLineNumber = this.getLineNumber();
       
       attribute = m_Tag.getAttribute("src");
       if ((attribute != null ))
       {
           //Jpeg or gif files should be used instead of bmp files
           result.sRuleId = "Att01";
           if (attribute.endsWith(".bmp") == true)
             result.bRuleViolated = true;
           else
             result.bRuleViolated = false;

           Manager.getInstance().storeToDB(result);
           
           String[] array = attribute.split("/");
           for (int j = 0; j < array.length; j++)
           {
               bLowerCase = true;   
               for (int i = 0; i < array[j].length(); i++)
               {
                 if ((Character.isLetter(array[j].charAt(i)) == true)
                         && (Character.isLowerCase(array[j].charAt(i)) == false))
                 {
                   bLowerCase = false;
                   break;
                 }
               }
               result.sRuleId = "Att02";
               if (bLowerCase == false)               
                 result.bRuleViolated = true;
               else
                 result.bRuleViolated = false;
               Manager.getInstance().storeToDB(result);
              //////////////////////////////
               result.sRuleId = "Att03";
               if (array[j].contains(" ") == true)               
                  result.bRuleViolated = true;
               else
                  result.bRuleViolated = false;
               Manager.getInstance().storeToDB(result);

               /////////////////////////////
               result.sRuleId = "Att04";
               if (array[j].length() >= 20)               
                  result.bRuleViolated = true;
               else
                  result.bRuleViolated = false;

               Manager.getInstance().storeToDB(result);
           }
       }
       
       //If mouseover event is used, keyboard activation should be applicable too.
       attribute = m_Tag.getAttribute("onmouseover");
       if (attribute != null)
       {
           result.sRuleId = "Att05";

           String sOnFocus;
           sOnFocus = m_Tag.getAttribute("onFocus");

           if ((sOnFocus == null) || ((sOnFocus != null) && (sOnFocus.compareToIgnoreCase(attribute) != 0)))                         
              result.bRuleViolated = true;         
           else          
               result.bRuleViolated = false;    

           Manager.getInstance().storeToDB(result);
       }
       
    }
}
